package proxy.jdk;

import proxy.IPerson;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * User:xuzhidian
 * Date:2018/4/2
 * Time:22:55
 */
public class JdkScalper implements InvocationHandler {
    //被代理对象
    private IPerson person;

    public Object getInstance(IPerson person) {
        this.person = person;
        Class<? extends IPerson> aClass = person.getClass();
        //用来生成一个新的对象(字节码重组)
        return Proxy.newProxyInstance(aClass.getClassLoader(), new Class[]{IPerson.class}, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是黄牛");
        System.out.println("排队");
        method.invoke(this.person, args);
        System.out.println("买票结束");
        return null;
    }
}
